<template>
	<el-container class="el-container">
		<el-header class="el-header">
			<div>
				<img src="../assets/heima.png">
				<span>电商管理系统</span>
			</div>
			<el-button class="btn-logout" type="info" round @click="logout">退出</el-button>
		</el-header>
		<el-container>
			<!-- 这里是侧边栏 -->
			<el-aside class="el-aside" :width="iscollapse ? '64px' : '200px'">
				<div @click="toggleCollapse" class="toggle-button">|||</div>
				<!-- 侧边栏菜单区域 -->
				<el-menu :default-active="activePath" router :collapse-transition="false" :collapse="iscollapse" :unique-opened="true" background-color="#333744" text-color="#fff" active-text-color="#409EFF">
					<!-- 一级菜单 -->
					<el-submenu v-for="item in meunsList" :key="item.id" :index="'/' + item.path">
						<!-- 一级菜单的模板区域 -->
						<template slot="title">
							<i :class="iconObj[item.id + '']"></i>
							<span>{{item.authName}}</span>
						</template>
						<!-- 二级菜单 -->
						<el-menu-item @click="saveNavState('/' + subItem.path)" v-for="subItem in item.children" :key="subItem.id" :index="'/' + subItem.path">
							<!-- 二级菜单的模板区域 -->
							<template slot="title">
								<i class="el-icon-menu"></i>
								<span>{{subItem.authName}}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 这里是主体区 -->
			<el-main class="el-main">
				<router-view></router-view>
			</el-main>
		</el-container>
	</el-container>

</template>
<script>
	export default {
		data () {
			return {
				activePath: '',
				iscollapse: false,
				meunsList: [],
				iconObj: {
					102: 'iconfont icon-danju',
					103: 'iconfont icon-tijikongjian',
					101: 'iconfont icon-shangpin',
					125: 'iconfont icon-user',
					145: 'iconfont icon-baobiao'
				}
			}
		},
		methods: {
			logout () {
				// this.$message.error('1')
				window.sessionStorage.clear()
				this.$router.push('/login')
			},
			async getMeuns () {
				// this.$message.success('getMeuns')
				// 在这里写上请求
				const { data: res } = await this.$http.get('menus')
				// console.log(res)
				if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
				this.meunsList = res.data
				// console.log(this.meunsList)
			},
			toggleCollapse () {
				// this.$message.success('toggleCollapse')
				this.iscollapse = !this.iscollapse
			},
			saveNavState (subitemPath) {
				// this.$message.success('saveNavState')
				// this.activeIndex =
				// this.$message.success(subitemPath)
				window.sessionStorage.setItem('activePath', subitemPath)
				this.activePath = subitemPath
			}
		},
		created () {
			this.activePath = window.sessionStorage.getItem('activePath') || '/users'
			this.getMeuns()
			// this.$message.success(this.activeIndex)
		}
	}
</script>

<style lang="less" scoped>
	.el-header {
		background-color: #373d41;
		display: flex;
		justify-content: space-between;
		padding-left: 0;
		align-items: center;
		color: #fff;
		font-size: 20px;

		>div {
			display: flex;
			align-items: center;

			span {
				margin-left: 15px
			}
		}
	}

	.el-aside {
		background-color: #333744;
		.el-menu {
			border-right: none
		}
		.toggle-button {
			background-color: #4a5064;
			font-size: 10px;
			line-height: 24px;
			color: #fff;
			text-align: center;
			letter-spacing: 0.2em;
			cursor: pointer;
		}
	}

	.el-main {
		background-color: #EAEDF1
	}

	.el-container {
		height: 100%
	}

	.btn-logout {}
</style>
